package com.leisai.manager.controller.system;

import com.leisai.manager.pojo.Temp;
import com.leisai.manager.response.PageDataResult;
import com.leisai.manager.service.IndustryService;
import com.leisai.manager.service.TempService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;


@Controller
@RequestMapping("temp")
public class TempController  {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private TempService tempService;
    @Autowired
    private IndustryService industryService;


    /**
     *
     * 功能描述: 跳到模板管理
     *
     * @param:
     * @return:
     * @auther: jim
     * @date: 2021-11-23
     */
    @RequestMapping("tempManage")
    public String tempManage() {
        logger.info("进入模板管理");
        return "temp/tempManage";
    }

    /**
     *
     * 功能描述: 跳到模板管理
     *
     * @param:
     * @return:
     * @auther: jim
     * @date: 2021-11-23
     */
    @RequestMapping("addTempPage")
    public String addTempPage(Temp temp ,Model model) {
        logger.info("进入模板编辑");
        model.addAttribute("tempid",temp.getTempId());
        model.addAttribute("content",temp.getContent());
        model.addAttribute("tempTitle",temp.getTempTitle());
        model.addAttribute("sort",temp.getSort());
        return "temp/addTemp";
    }


    /**
     *
     * 功能描述: 获取模板列表
     *
     * @param:
     * @return:
     * @auther: jim
     * @date: 2021/11/30 10:30
     */
    @PostMapping("tempList")
    @ResponseBody
    public PageDataResult tempList(@RequestParam("pageNum") Integer pageNum,
                                         @RequestParam("pageSize") Integer pageSize){
        logger.info("获取模板列表");
        PageDataResult pdr = new PageDataResult();
        try {
            if(null == pageNum) {
                pageNum = 1;
            }
            if(null == pageSize) {
                pageSize = 10;
            }
            // 获取服务类目列表
            pdr = tempService.getTempList(pageNum ,pageSize);
            logger.info("模板列表查询=pdr:" + pdr);

        } catch (Exception e) {
            e.printStackTrace();
            logger.error("模板列表查询异常！", e);
        }
        return pdr;
    }

    /**
     *
     * 功能描述:设置模板[新增或更新]
     *
     * @param:
     * @return:
     * @auther: jim
     * @date: 2021/12/01 9:42
     */
    @PostMapping("setTemp")
    @ResponseBody
    public Map<String,Object> setTemp(Temp temp) {
        logger.info("设置模板[新增或更新]！temp:" + temp);
        Map<String,Object> data = new HashMap();
        if(temp.getTempId() == null || temp.getTempId().equals("") ){
            //新增模板
            data = tempService.addTemp(temp);
        }else{
            //修改模板
            data = tempService.updateTemp(temp);
        }
        return data;
    }

    /**
     *
     * 功能描述: 删除模板
     *
     * @param:
     * @return:
     * @auther: youqing
     * @date: 2018/11/30 12:02
     */
    @PostMapping("del")
    @ResponseBody
    public Map<String, Object> del(Temp temp) {
        logger.info("删除模板！id:" + temp.getTempId());
        temp.setStatus(0);
        Map<String, Object> data = new HashMap<>();
        //删除模板(软删除）
        data = tempService.setStatus(temp);
        return data;
    }
    /**
     *
     * 功能描述: 删除模板
     *
     * @param:
     * @return:
     * @auther: youqing
     * @date: 2018/11/30 12:02
     */
    @RequestMapping("tempDetail")
    public String tempDetail(Temp temp ,Model model) {
        logger.info("查看模板！id:"+temp.getTempId());
        model.addAttribute("temp",temp);

        return "temp/tempDetail";
    }

}
